<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>宠物列表</title>
    <link rel="stylesheet" href="../assets/css/layui.css" media="all">
    <link rel="stylesheet" href="../assets/css/view.css"/>
    <link rel="stylesheet" href="../assets/css/admin.css"/>
    <script src="../assets/jquery.min.js"></script>
    <script src="../assets/layui.all.js" charset="utf-8"></script>
</head>
<body class="layui-view-body">
<div class="layui-content">
    <!--面包屑导航-->
    <div class="mianb">
        <div class="mblf layui-col-xs6 layui-col-md6">
            <i class="layui-icon">&#xe656;</i>
            <p>宠物管理 > <span>宠物列表</span></p>
        </div>
    </div>
    <!--面包屑导航-->
    <div class="layui-row">
        <div class="layui-card">
            <div class="table-responsive">

                <table class="layui-hide" id="test" lay-filter="test"></table>

                <script type="text/html" id="toolbarDemo">
                    <div class="layui-btn-container">
                        <button class="layui-btn layui-btn-sm" lay-event="grounding">宠物上架</button>
                        <button class="layui-btn layui-btn-sm" lay-event="undercarriage">宠物下架</button>
                        <button class="layui-btn layui-btn-sm" lay-event="delete">宠物删除</button>
                    </div>
                </script>

                <script type="text/html" id="barDemo">
                    <a class="layui-btn layui-btn-xs" lay-event="look">查看</a>
                </script>

            </div>

        </div>
    </div>
</div>

<script>
    layui.use(['table', 'layer', 'jquery'], function () {
        var table = layui.table,
            $ = layui.jquery,
            layer = layui.layer;

        table.render({
            elem: '#test'
            //发送请求
            , url: '/getGoodsList'
            , cellMinWidth: 50
            , limit: 8
            , limits: [8, 16, 24, 32, 40]
            , method: "GET"
            , text: {
                none: '暂时没有数据哦！'
            }
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print']
            , title: '宠物数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {type: 'numbers', fixed: 'left', title: '序号', align: 'center'}
                , {field: 'goodsName', title: '宠物名称', align: 'center'}
                , {field: 'goodsPrice', title: '宠物价格', sort: true, align: 'center'}
                , {field: 'type1Id', title: '宠物类型', align: 'center'}
                , {field: 'type3Id', title: '宠物品种', align: 'center'}
                , {field: 'goodsDesc', title: '宠物描述', align: 'center'}
                , {
                    field: 'goodsStatus', title: '是否上架', sort: true, align: 'center', templet: function (d) {
                        if (d.goodsStatus === 1) {
                            return "<span style='color: green'>√</span>";
                        } else {
                            return "<span style='color: red'>x</span>";
                        }
                    }
                }
                , {fixed: 'right', title: '操作', align: 'center', toolbar: '#barDemo'}
            ]]
            , page: true
            , id: "testId"
        });


        /*搜索事件处理*/
        $('#search').click(function () {
            var username = $('#goodsName').val();
            if (username === "" && type === "" && dieWhy === "") {
                layer.msg("请输入要搜索的内容！");
                return;
            }
            /*表格重载*/
            table.reload('testId', {
                page: {
                    curr: 1
                },
                where: {
                    username: username
                }
            }, 'data');
        });


        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
                //获取被选中的信息
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'grounding':
                        if (checkStatus.data.length === 0) {
                            layer.msg("请选择要上架宠物的信息!", {icon: 2, time: 800});
                        } else {
                            layer.confirm('确定上架选中宠物的信息吗？', {icon: 3}, function (index) {
                                layer.close(index);
                                index = layer.load(1, {shade: [0.1, '#fff']});
                                //存放要上架的ID
                                var ids = '';
                                for (var i = 0; i < checkStatus.data.length; i++) {
                                    ids += checkStatus.data[i].goodsId + ",";
                                }
                                parent.layer.msg('正在上架...', {icon: 16, shade: 0.3, time: 5000});
                                $.ajax({
                                    type: 'PUT',
                                    data: {ids: ids},
                                    url: '/batchGrounding',
                                    error: function () {
                                        layer.closeAll('loading');
                                        layer.msg('系统错误!', {icon: 2, time: 800, shade: 0.2});
                                        layer.close(index);
                                    },
                                    success: function (result) {
                                        layer.closeAll('loading');
                                        if (result === 'success') {
                                            parent.layer.msg('上架成功!', {
                                                icon: 1, time: 2000, shade: 0.2, end: function () {
                                                    window.location.reload();
                                                }
                                            });
                                        } else {
                                            parent.layer.msg('上架失败,请重试!', {
                                                icon: 5, time: 1000, end: function () {
                                                    window.location.reload()
                                                }
                                            });
                                        }
                                        layer.close(index);
                                    }
                                });
                            });
                        }
                        break;
                    case 'undercarriage':
                        if (checkStatus.data.length === 0) {
                            layer.msg("请选择要下架宠物的信息!", {icon: 2, time: 800});
                        } else {
                            layer.confirm('确定下架选中宠物的信息吗？', {icon: 3}, function (index) {
                                layer.close(index);
                                index = layer.load(1, {shade: [0.1, '#fff']});
                                //存放要上架的ID
                                var ids = '';
                                for (var i = 0; i < checkStatus.data.length; i++) {
                                    ids += checkStatus.data[i].goodsId + ",";
                                }
                                parent.layer.msg('正在下架...', {icon: 16, shade: 0.3, time: 5000});
                                $.ajax({
                                    type: 'PUT',
                                    data: {ids: ids},
                                    url: '/batchUndercarriage',
                                    error: function () {
                                        layer.closeAll('loading');
                                        layer.msg('系统错误!', {icon: 2, time: 800, shade: 0.2});
                                        layer.close(index);
                                    },
                                    success: function (result) {
                                        layer.closeAll('loading');
                                        if (result === 'success') {
                                            parent.layer.msg('下架成功！', {
                                                icon: 1, time: 2000, shade: 0.2, end: function () {
                                                    window.location.reload();
                                                }
                                            });
                                        } else {
                                            parent.layer.msg('下架失败,请重试!', {
                                                icon: 5, time: 1000, end: function () {
                                                    window.location.reload()
                                                }
                                            });
                                        }
                                        layer.close(index);
                                    }
                                });
                            });
                        }
                        break;
                    case 'delete':
                        if (checkStatus.data.length === 0) {
                            layer.msg("请选择要删除宠物的信息!", {icon: 2, time: 800});
                        } else {
                            layer.confirm('确定删除选中宠物的信息吗？', {icon: 3}, function (index) {
                                layer.close(index);
                                index = layer.load(1, {shade: [0.1, '#fff']});
                                //存放要删除的ID
                                var ids = '';
                                for (var i = 0; i < checkStatus.data.length; i++) {
                                    ids += checkStatus.data[i].goodsId + ",";
                                }
                                parent.layer.msg('正在删除...', {icon: 16, shade: 0.3, time: 5000});
                                $.ajax({
                                    type: 'DELETE',
                                    data: {ids: ids},
                                    url: '/batchDelete',
                                    error: function () {
                                        layer.closeAll('loading');
                                        layer.msg('系统错误!', {icon: 2, time: 800, shade: 0.2});
                                        layer.close(index);
                                    },
                                    success: function (result) {
                                        layer.closeAll('loading');
                                        if (result === 'success') {
                                            parent.layer.msg('删除成功！', {
                                                icon: 1, time: 2000, shade: 0.2, end: function () {
                                                    window.location.reload();
                                                }
                                            });
                                        } else {
                                            parent.layer.msg('删除失败,请重试!', {
                                                icon: 5, time: 1000, end: function () {
                                                    window.location.reload()
                                                }
                                            });
                                        }
                                        layer.close(index);
                                    }
                                });
                            });
                        }
                        break;
                }
            }
        );

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'look') {
                var index = layer.open({
                    type: 2,
                    title: '宠物信息',
                    fix: false,
                    shadeClose: true,
                    shade: 0.8,
                    area: ['900px', '600px'],
                    content: '/look',
                    success: function (layero, index) {
                        $.ajax({
                            type: 'GET',
                            url: '/getGoodsById/' + data.goodsId,
                            dataType: 'json',
                            error: function () {
                                layer.msg('系统错误!', {icon: 2, time: 800, shade: 0.2});
                            },
                            success: function (result) {
                                var body = layer.getChildFrame('body', index);
                                var iframeWin = window[layero.find('iframe')[0]['name']];
                                //将数据填充到子页面上
                                body.find('#goodsName').text(data.goodsName);
                                body.find('#goodsPrice').text(data.goodsPrice + "元");
                                layer.msg(data.type1Id);
                                body.find('#type1Id').text(data.type1Id);
                                body.find('#type3Id').text(data.type3Id);
                                body.find('#goodsDesc').text(data.goodsDesc);
                                body.find('#goodsImage').attr('src', data.goodsImage);
                            }
                        });

                    },
                    end: function () {
                        return false;
                    }
                });
            }
        });
    });
</script>


</body>
</html>